/**
 * 
 */
package com.changil.nowonchangilchurch.page.word;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.common.Constants;
import com.changil.nowonchangilchurch.page.BaseActivity;
import com.changil.nowonchangilchurch.page.BaseNetworkFragment;
import com.changil.nowonchangilchurch.page.LatestDetailFragment;
import com.changil.nowonchangilchurch.ui.item.Header.UserActionListener;

/**
 * @author zeropol2
 *
 */
public class WordActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	protected void initLayout() {
		mHeader.setHeaderText(getString(R.string.word));
		mHeader.setHeaderColor(R.color.color_word_n);
		mHeader.setShareBtnEnabled(true);
		mHeader.setUserActionListener(new UserActionListener() {
			
			@Override
			public void onShareBtnClick() {
				startActivity(Intent.createChooser(createShareIntent(), getString(R.string.share)));
			}
		});
		mTabs.setIndicatorColorResource(R.color.color_word_n);
	}

	protected void defineAdapter() {
		mAdapter = new WordPagerAdapter(getSupportFragmentManager());
	}
	
	
	private LatestDetailFragment mFragment1 = null;
	private LatestDetailFragment mFragment2 = null;
	private LatestDetailFragment mFragment3 = null;
	private LatestDetailFragment mFragment4 = null;
	private class WordPagerAdapter extends FragmentPagerAdapter {
		private final String[] TITLES = { getString(R.string.this_week_bible_word),
				getString(R.string.last_week_word),
				getString(R.string.graceful_word),
				getString(R.string.pray_word)};

		public WordPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public int getCount() {
			return TITLES.length;
		}

		@Override
		public Fragment getItem(int position) {
			if(0 == position) {
				if(null == mFragment1) {
					mFragment1 = LatestDetailFragment.newInstance(Constants.BOARD_ID_THIS_WEEK_BIBLE);
					((BaseNetworkFragment)mFragment1).setForceDataLoad(true);
				}
				return mFragment1;
			} else if( 1 == position) {
				if(null == mFragment2) {
					mFragment2 = LatestDetailFragment.newInstance(Constants.BOARD_ID_LAST_WEEK_WORD);
				}
				return mFragment2;
			} else if( 2 == position) {
				if(null == mFragment3) {
					mFragment3 = LatestDetailFragment.newInstance(Constants.BOARD_ID_GRACEFUL_WORD);
				}
				return mFragment3;
			} else if( 3 == position) {
				if(null == mFragment4) {
					mFragment4 = LatestDetailFragment.newInstance(Constants.BOARD_ID_PRAY);
				}
				return mFragment4;
			}
			return null;
		}

	}
	
	private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        LatestDetailFragment fr = (LatestDetailFragment)mAdapter.getItem(mPager.getCurrentItem());
        shareIntent.putExtra(Intent.EXTRA_TEXT, fr.getContent());
        return shareIntent;
    }
}
